package com.newture.lxz.services.impl;

import com.newture.lxz.constant.Constant;
import com.newture.lxz.db.mapper.LdGoodsMapper;
import com.newture.lxz.db.mapper.LdTagRelationshipMapper;
import com.newture.lxz.db.model.LdGoods;
import com.newture.lxz.db.model.LdGoodsExample;
import com.newture.lxz.errors.AppException;
import com.newture.lxz.errors.Errors;
import com.newture.lxz.services.IDocumentsSrv;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Clearlove8
 */
@Service("DocumentsSrv")
public class DocumentsSrv implements IDocumentsSrv {

    @Resource
    LdGoodsMapper ldGoodsMapper;
    @Resource
    LdTagRelationshipMapper ldTagRelationshipMapper;

    @Override
    public LdGoods findById(int id, String[] columns) {
        LdGoodsExample example = new LdGoodsExample();
        LdGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        criteria.andIsDeletedEqualTo(Constant.NO);


        example.setDynamicColumns(columns);
        List<LdGoods> list = ldGoodsMapper.selectByExample(example);
        if(list.isEmpty()) {
//            throw new AppException(Errors.NO_RECORD_FOUND_BY_ID.name(),
//                    String.format("ID is %s", id), "No record found in LdGoodsSrv.findById()");
            return null;
        }

        if(list.size() > 1) {
            throw new AppException(Errors.MORE_THAN_ONE_RECORDS_FOUND.name(),
                    String.format("ID is %s", id), "More than one record found in LdGoods.findById()");
        }

        return list.get(0);
    }



}

